<template>
    <el-form ref="formRef" style="min-width: 600px" :model="releaseSettingsForm" label-width="auto" label-position="top"
        class="demo-ruleForm">
        <el-form-item label="标题" prop="issueTitle" :rules="[
            { required: true, message: 'issueTitle is required' }
        ]">
            <el-input v-model="releaseSettingsForm.issueTitle" type="text" autocomplete="off" />
        </el-form-item>


        <div class="flex items-end">

            <el-form-item label="封面和摘要" prop="issueCoverImage" :rules="[
                { required: true, message: 'issueCoverImage is required' }
            ]">
                    <Upload v-model="releaseSettingsForm.issueCoverImage"/>
            </el-form-item>


            <el-form-item prop="issueDescription" :rules="[
                { required: true, message: 'issueDescription is required' }
            ]"
            >
                    <el-input type="textarea" v-model="releaseSettingsForm.issueDescription" :rows="4" style="width:500px ;margin-left:20px;" />
            </el-form-item>

        </div>

        <el-form-item label="发送方式" prop="pubway" :rules="[
            { required: true, message: '请选择发送方式' }
        ]">
            <el-radio-group v-model="releaseSettingsForm.pubway">
                <el-radio value="immediate">立即发送</el-radio>
                <el-radio value="wait">定时发送</el-radio>
            </el-radio-group>
        </el-form-item>


        <el-form-item label="发送时间" v-if="releaseSettingsForm.pubway === 'wait'" prop="waitPubTime" :rules="[
            { required: true, message: '请选择发送时间', trigger: 'change' }
        ]">
            <el-date-picker v-model="releaseSettingsForm.waitPubTime" type="datetime"
                placeholder="Select date and time" />
        </el-form-item>

        
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">发布</el-button>
            <el-button @click="goPrevStep">上一步</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted, watch } from 'vue'

import type { FormInstance } from 'element-plus'
import Upload from '@/components/Upload.vue'
import { publishReleaseList } from '@/api/newsApi'
import { newsIssueTemplate } from '@/api/newsApi'



const formRef = ref<FormInstance>()

const props = defineProps({
    activeStep: {
        type: Number,
        default: 1
    },
    prevStep: {
        type: Function,
        required: true
    }
})

const emit = defineEmits(['step-change'])

// {
//   "waitPubTime": "2024-01-02 10:00:00",
//   "issueTitle": "能源行业周报（第六期）",
//   "issueCoverImage": "https://example.com/issue-6-cover.png",
//   "issueDescription": "本期精选10条行业关键信息",
//   "issuePublisher": "admin"
// }

const releaseSettingsForm = reactive({
    issueTitle: '',
    pubway: '',
    waitPubTime: '',
    issueCoverImage: '',
    issueDescription: '',
    issuePublisher: 'admin',
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log('submit!')
            publishReleaseList(releaseSettingsForm).then(res => {
                console.log('publishReleaseList', res)
            })
        } else {
            console.log('error submit!')
        }
    })
}

watch(() => releaseSettingsForm.issueCoverImage, (newVal, oldVal) => {
    console.log(newVal, oldVal)
})



const goPrevStep = () => {
    // 调用父组件传递的prevStep方法
    if (typeof props.prevStep === 'function') {
        props.prevStep()
    }
}

onMounted(() => {
    newsIssueTemplate().then(res => {
        console.log('newsIssueTemplate', res)
        releaseSettingsForm.issueTitle = res.data.template
    })
})
</script>
